package com.base.control.base;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.common.constant.RedisKey;
import com.base.control.model.EsDictionariesDto;
import com.base.domain.module.vo.DictionariesVo;
import com.base.domain.module.vo.EsDictionariesVo;
import com.base.domain.service.IDictionariesService;
import com.redis.sdk.cachecore.CacheLocalDelete;
import com.redis.sdk.cachecore.CacheRedisDelete;
import com.global.RAdviceIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * ES 词库模块
 * </p>
 *
 * @author cyj
 * @since 2021-12-24
 */
@RestController
@RequestMapping("/es")
@Slf4j
@Api(tags = "es词库管理")
public class EsKeywordController {

    @Autowired
    private IDictionariesService dictionariesService;

    @Autowired
    private DictionariesController dictionariesController;

    public final String keyword = "es_keyword";

    public final String keywordStop = "es_keyword_stop";


    @GetMapping("/page/keyword")
    @ApiOperation("es分词库--分页查询")
    public IPage<EsDictionariesVo> keywordPage(@RequestParam(value = "keyword", required = false) String word, Page page) {
        return dictionariesService.keyword(this.keyword, word, page);
    }

    @GetMapping("/page/keyword-stop")
    @ApiOperation("es停用分词库--分页查询")
    public IPage<EsDictionariesVo> keywordStopPage(@RequestParam(value = "keyword", required = false) String word, Page page) {
        return dictionariesService.keyword(this.keywordStop, word, page);
    }

    @PostMapping("/add")
    @ApiOperation("es分词库--新增")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = keyword)
    @CacheLocalDelete(value = keyword)
    public void add(@RequestBody List<EsDictionariesDto> dictionariesDto) {
        dictionariesService.addes(keyword, dictionariesDto);
    }

    @PostMapping("/add-stop")
    @ApiOperation("es停用分词库--新增")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = keywordStop)
    @CacheLocalDelete(value = keywordStop)
    public void addStop(@RequestBody List<EsDictionariesDto> dictionariesDto) {
        dictionariesService.addes(keywordStop, dictionariesDto);
    }

    @DeleteMapping("/delete")
    @ApiOperation("es分词--删除")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = keyword)
    @CacheLocalDelete(value = keyword)
    public void delete(@RequestParam("id") Integer id) {
        dictionariesService.delete(id);
    }

    @DeleteMapping("/delete-stop")
    @ApiOperation("es停用分词--删除")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = keywordStop)
    @CacheLocalDelete(value = keywordStop)
    public void deleteStop(@RequestParam("id") Integer id) {
        dictionariesService.delete(id);
    }

    @GetMapping("/keyword")
    @ApiOperation("es分词库--remote_ext_dict")
    @RAdviceIgnore
    public String keyword(HttpServletResponse response) {
        List<DictionariesVo> dictionariesVos = dictionariesController.group(keyword);
        String join = dictionariesService.addEsHead(keyword, dictionariesVos, response);
        return join;
    }

    @GetMapping("/keyword-stop")
    @ApiOperation("es停用分词--remote_ext_stopwords")
    @RAdviceIgnore
    public String keywordStop(HttpServletResponse response) {
        List<DictionariesVo> dictionariesVos = dictionariesController.group(keywordStop);
        String join =  dictionariesService.addEsHead(keyword, dictionariesVos, response);
        return join;
    }
}

